<?PHP
$media=new music;

if(!empty($_GET['id']))
{	
	$playlist_queue=$media->get_playlist_queue($_GET['id']);
	if(!empty($playlist_queue))
	 $queue_items = explode(",",$playlist_queue);
}

?>
<script type="text/javascript"> 
// When the document is ready set up our sortable with it's inherant function(s) 
$(document).ready(function() { 
  $("#test-list").sortable({ 
    handle : '.handle', 
    update : function () { 
	  var order_string ="";
	  
	  var i;
	  for(i=1;i<=$("#queue_table tr").length;i++)
	  {
		var order_string =order_string +$("#queue_table tr:nth-child("+i+")").children(":nth-child(1)").html();
		if(i!=$("#queue_table tr").length)
	     var order_string =order_string+",";
		 
	  }
	  //alert(order_string);	  
      $("#test-log").attr("value",order_string);

    } 
  }); 
	
}); 
</script>
<div class="queue-buttons">
 <button id="queue-song">song</button>
 <button id="queue-vad">video_ad</button>
 <button id="queue-aad">audio_ad</button>
 <button id="queue-loop">loop</button>
 <button id="queue-stinger">stinger</button>
</div>

	<div class="formholder">
	<div class="datatop">

	Playlist Queue for <?PHP echo $media->playlist_name;  ?>
	</div>
<div class="datalabel" id="info">Move around to update</div>

<div class="datadata">
<table id="queue_table" class="tablesorter song_playlist" cellspacing="0"><tbody id="test-list" style='clear:both;'> 

	
<?PHP

foreach($queue_items as $x)
{

	?>
	<tr> 
	<td><?PHP echo $x; ?></td>

	<td class='cancel'>Remove</td>
	<td class='handle'>Move</td>
  	</tr>
	<?

}

?>
</tbody> </table>

  <input type="hidden" name="test-log" id="test-log" /> 


</div><div class="datafootbg">
	<button id="save-queue">Save Queue</button>
</div></div>

<div id='results'></div>

</div>


<script type="text/javascript" charset="utf-8">
	$(function() {
	
		$("#queue-song").click(function(){
					
			$("#queue_table").append("<tr><td>song</td><td id='cancel'>Remove</td><td class='handle'>Move</td></tr>");
			var order_string ="";
	  
			var i;
			for(i=1;i<=$("#queue_table tr").length;i++)
			{
				var order_string =order_string +$("#queue_table tr:nth-child("+i+")").children(":nth-child(1)").html();
				if(i!=$("#queue_table tr").length)
					var order_string =order_string+",";
		 
			}
			//alert(order_string);	  
			$("#test-log").attr("value",order_string);
		});
		
		$("#queue-vad").click(function(){
					
			$("#queue_table").append("<tr><td>video_ad</td><td class='cancel'>Remove</td><td class='handle'>Move</td></tr>");
			var order_string ="";
	  
			var i;
			for(i=1;i<=$("#queue_table tr").length;i++)
			{
				var order_string =order_string +$("#queue_table tr:nth-child("+i+")").children(":nth-child(1)").html();
				if(i!=$("#queue_table tr").length)
				var order_string =order_string+",";
			 
			}
			//alert(order_string);	  
			$("#test-log").attr("value",order_string);
		});
		$("#queue-aad").click(function(){
					
			$("#queue_table").append("<tr><td>audio_ad</td><td class='cancel'>Remove</td><td class='handle'>Move</td></tr>");
			var order_string ="";
	  
			var i;
			for(i=1;i<=$("#queue_table tr").length;i++)
			{
				var order_string =order_string +$("#queue_table tr:nth-child("+i+")").children(":nth-child(1)").html();
				if(i!=$("#queue_table tr").length)
				var order_string =order_string+",";
			 
			}
			//alert(order_string);	  
			$("#test-log").attr("value",order_string);
		});
		$("#queue-loop").click(function(){
					
			$("#queue_table").append("<tr><td>loop</td><td class='cancel'>Remove</td><td class='handle'>Move</td></tr>");
			var order_string ="";
	  
			var i;
			for(i=1;i<=$("#queue_table tr").length;i++)
			{
				var order_string =order_string +$("#queue_table tr:nth-child("+i+")").children(":nth-child(1)").html();
				if(i!=$("#queue_table tr").length)
				var order_string =order_string+",";
			 
			}
			//alert(order_string);	  
			$("#test-log").attr("value",order_string);
		});	
		$("#queue-stinger").click(function(){
					
			$("#queue_table").append("<tr><td>stinger</td><td class='cancel'>Remove</td><td class='handle'>Move</td></tr>");
			var order_string ="";
	  
			var i;
			for(i=1;i<=$("#queue_table tr").length;i++)
			{
				var order_string =order_string +$("#queue_table tr:nth-child("+i+")").children(":nth-child(1)").html();
				if(i!=$("#queue_table tr").length)
				var order_string =order_string+",";
			 
			}
			//alert(order_string);	  
			$("#test-log").attr("value",order_string);
		});
		$("#save-queue").click(function(){
		    var string = $("#test-log").attr("value");	
			$.ajax({
            type: "POST",
            url: "m/music/a_save_sequence.php",
            data: { playlist: "<?PHP echo $_GET['id']; ?>",sequence:string } ,
            dataType: "json",
			success: function(data){
			 alert("Queue Saved");
			}
			});
			
		});
		
		$(".cancel").live("click", function(){
			$(this).parent("tr:first").slideUp().remove();
			
			var order_string ="";
	  
			var i;
			for(i=1;i<=$("#queue_table tr").length;i++)
			{
				var order_string =order_string +$("#queue_table tr:nth-child("+i+")").children(":nth-child(1)").html();
				if(i!=$("#queue_table tr").length)
					var order_string =order_string+",";
		 
			}
			//alert(order_string);	  
			$("#test-log").attr("value",order_string);
		});
	
	});
</script>
